package com.open.basic.util;

import com.open.basic.thread.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Function;
import java.util.function.Supplier;


/**
 * @author: system
 * @Date: 2024/11/17 下午12:54
 * @Version: V1.0
 * @description: |
 */

@Slf4j
public class CompletableFutureUtil {

    private static final ThreadPoolExecutor EXECUTOR = ThreadPoolUtil.getNewThreadPool("completable-pool", 10);


    public static void runAsync(Runnable runnable) {
        CompletableFuture.runAsync(runnable, EXECUTOR);
    }

    public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) {
        return CompletableFuture.supplyAsync(supplier, EXECUTOR);
    }



    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Runnable runnable = () -> {
            log.info("start task!");
        };
        runAsync(runnable);

        Supplier<String> supplier = () -> {
            log.info("start task!");
            return "end";
        };
        CompletableFuture<String> stringCompletableFuture = supplyAsync(supplier);
        log.info("执行结果：{}", stringCompletableFuture.get());
    }
}
